﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Enyim.Collections
{
    public class Envelope
    {
        internal Envelope()
        {
        }

        public Envelope(double x1, double y1, double x2, double y2)
        {
            X1 = x1;
            Y1 = y1;
            X2 = x2;
            Y2 = y2;
        }

        public double X1 { get; private set; } // 0

        public double Y1 { get; private set; } // 1

        public double X2 { get; private set; } // 2

        public double Y2 { get; private set; } // 3

        internal double Area { get { return (X2 - X1) * (Y2 - Y1); } }

        internal double Margin { get { return (X2 - X1) + (Y2 - Y1); } }

        internal void Extend(Envelope by)
        {
            X1 = Math.Min(X1, by.X1);
            Y1 = Math.Min(Y1, by.Y1);
            X2 = Math.Max(X2, by.X2);
            Y2 = Math.Max(Y2, by.Y2);
        }

        public override string ToString()
        {
            return String.Format("{0},{1} - {2},{3}", X1, Y1, X2, Y2);
        }

        internal bool Intersects(Envelope b)
        {
            return b.X1 <= X2 && b.Y1 <= Y2 && b.X2 >= X1 && b.Y2 >= Y1;
        }

        internal bool Contains(Envelope b)
        {
            return X1 <= b.X1 && Y1 <= b.Y1 && b.X2 <= X2 && b.Y2 <= Y2;
        }
    }
}